-
Notifications
You must be signed in to change notification settings - Fork 18
Fix DFanso#112 : add commit message length validation #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
- Add validation for commit message subject line length (50-72 char limit) - Display warnings when messages exceed recommended limits - Integrate validation into generation, regeneration, and editing workflows
Fix DFanso#112 : add commit message length validation
WalkthroughAdds a commit message subject-length validator to the CLI flow and invokes it after generation, regeneration, and edits. Updates .gitignore to ignore commit-msg patterns. No exported API changes. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor User
participant CLI
participant Generator as Msg Generator
participant Validator as Length Validator
participant Editor
participant Logger
User->>CLI: create commit message
CLI->>Msg Generator: generate()
Msg Generator-->>CLI: message
CLI->>Validator: validate(subject line)
Validator-->>Logger: warn if >50 / >72
Logger-->>User: warnings (if any)
alt User regenerates
User->>CLI: regenerate
CLI->>Msg Generator: generate()
Msg Generator-->>CLI: message
CLI->>Validator: validate()
Validator-->>Logger: warnings (if any)
end
alt User edits
User->>CLI: open editor
CLI->>Editor: edit(message)
Editor-->>CLI: edited message
CLI->>Validator: validate()
Validator-->>Logger: warnings (if any)
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Suggested labels
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Nitpick comments (1)
cmd/cli/createMsg.go (1)
592-594: Remove redundant empty check.
strings.Splitalways returns at least one element (even for empty strings), and the function already returns early ifmessage == "", making this check unreachable.Apply this diff to remove the redundant check:
lines := strings.Split(message, "\n") - if len(lines) == 0 { - return - } - subjectLine := strings.TrimSpace(lines[0])
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
.gitignore(1 hunks)cmd/cli/createMsg.go(4 hunks)
🔇 Additional comments (4)
.gitignore (1)
33-33: LGTM!Adding
commit-msgto the ignore patterns is appropriate for temporary commit message files that may be created during the validation workflow.cmd/cli/createMsg.go (3)
132-132: LGTM!Validation call is appropriately placed after initial message generation.
194-194: LGTM!Validation call is appropriately placed after message regeneration.
206-206: LGTM!Validation call is appropriately placed after message editing.
| // validateCommitMessageLength checks if the commit message exceeds recommended length limits | ||
| // and displays appropriate warnings | ||
| func validateCommitMessageLength(message string) { | ||
| if message == "" { | ||
| return | ||
| } | ||
|
|
||
| lines := strings.Split(message, "\n") | ||
| if len(lines) == 0 { | ||
| return | ||
| } | ||
|
|
||
| subjectLine := strings.TrimSpace(lines[0]) | ||
| subjectLength := len(subjectLine) | ||
|
|
||
| // Git recommends subject lines be 50 characters or less, but allows up to 72 | ||
| const maxRecommendedLength = 50 | ||
| const maxAllowedLength = 72 | ||
|
|
||
| if subjectLength > maxAllowedLength { | ||
| pterm.Warning.Printf("Commit message subject line is %d characters (exceeds %d character limit)\n", subjectLength, maxAllowedLength) | ||
| pterm.Info.Println("Consider shortening the subject line for better readability") | ||
| } else if subjectLength > maxRecommendedLength { | ||
| pterm.Warning.Printf("Commit message subject line is %d characters (recommended limit is %d)\n", subjectLength, maxRecommendedLength) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
Verify test coverage for the new validation function.
Ensure that the new validateCommitMessageLength function has adequate test coverage, including edge cases such as empty messages, messages at boundary lengths (50, 72), multi-line messages, and messages with Unicode characters.
Run the following script to check for test coverage:
🏁 Script executed:
#!/bin/bash
# Description: Search for test files covering validateCommitMessageLength
# Search for test files related to createMsg
fd -e go -e test.go 'createMsg.*test'
# Search for test cases that reference the validation function
rg -n 'validateCommitMessageLength' --type=goLength of output: 408
Add unit tests for validateCommitMessageLength
No tests currently cover the function in cmd/cli/createMsg.go:586—add tests for empty input, boundary lengths (50,72 characters), multi-line messages, and Unicode characters.
🤖 Prompt for AI Agents
In cmd/cli/createMsg.go around lines 584 to 609, add unit tests for
validateCommitMessageLength covering empty input, subject exactly at the
recommended boundary (50 chars), exactly at the allowed boundary (72 chars),
just over those boundaries, a multi-line message (ensure only subject line is
measured), and messages containing Unicode characters to ensure length is
measured correctly; implement tests that capture pterm output (or inject a
logger) to assert warnings/info are printed only for the appropriate cases and
include assertions for no output on empty/valid inputs.
| } | ||
|
|
||
| subjectLine := strings.TrimSpace(lines[0]) | ||
| subjectLength := len(subjectLine) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Character count may be incorrect for Unicode text.
len(subjectLine) counts bytes, not Unicode characters. For commit messages containing multi-byte characters (emoji, CJK characters, etc.), this will report an inflated length.
Import unicode/utf8 at the top of the file and apply this diff:
- subjectLength := len(subjectLine)
+ subjectLength := utf8.RuneCountInString(subjectLine)📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| subjectLength := len(subjectLine) | |
| subjectLength := utf8.RuneCountInString(subjectLine) |
🤖 Prompt for AI Agents
In cmd/cli/createMsg.go around line 597, the code uses len(subjectLine) which
returns byte count and is incorrect for Unicode; import "unicode/utf8" at the
top of the file and replace the byte-length call with
utf8.RuneCountInString(subjectLine) (assign result to the same variable name or
a new one) so the character count reflects Unicode runes rather than bytes.
DFanso
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 🎊
Summary by CodeRabbit
New Features
Chores